<template>
  <div
    :class="['parse', store.className ? store.className : '']"
    v-if="initStore.root"
  >
    <el-form
      ref="form"
      :label-position="initStore.root.config.labelPosition"
      :label-width="initStore.root.config.labelWidth + 'px'"
      :model="initStore.models"
    >
      <template v-for="(el, i) in initStore.root.list">
        <widget
          :element="el"
          :index="i"
          :key="el.key"
          :data.sync="initStore.root.list"
        ></widget>
      </template>

      <div>
        <template v-for="(el, i) in initStore.root.blackList">
          <widget
            :element="el"
            :index="i"
            :key="el.key"
            :data.sync="initStore.root.blackList"
          ></widget>
        </template>
      </div>
    </el-form>
  </div>
</template>

<script>
import { createStore } from "@/low-code/utils/store.js";
export default {
  name: "parse",
  props: ["store", "params"],
  data() {
    return {
      initStore: {},
    };
  },
  provide() {
    return {
      model: "preview", //界面
      getStore: () => {
        return this.initStore;
      },
    };
  },
  mounted() {
    let _this = this;
    let store = createStore(this.store);
    store.params = this.params;
    this.initStore = store;
    this.initStore.eventCenter.on("submit", (form) => {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$message({
            showClose: true,
            message: "恭喜你，这是一条成功消息",
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: "错了哦，这是一条错误消息",
            type: "error",
          });
        }
      });
      console.log("form", form);
    });
    this.$emit("storeInit", store);

    this.initStore.controller.verify = function () {
      return new Promise((resolve) => {
        _this.$refs["form"].validate((valid) => {
          if (valid) {
            resolve({
              isSuccess: true,
            });
          } else {
            resolve({
              isSuccess: false,
            });
          }
        });
      });
    };
    this.initStore.controller.resetFields = this.$refs["form"].resetFields.bind(this);

    this.initStore.controller.submit=this.submit.bind(this);

  },
  methods: {
    submit() {
      let _this = this;
      return new Promise((resolve) => {
        _this.$refs["form"].validate((valid) => {
          if (valid) {
            setTimeout(()=>{
             resolve({
              isSuccess: true,
              result:null
            });
            },500)
          } else {
             resolve({
              isSuccess: false,
              errorMessage:'校验失败'
            });
          }
        });
      });
    },
  },
  destroyed() {
    this.initStore.lifeCycle.destroyed();
  },
};
</script>

<style  scoped>
</style>